{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Using submodels in PyBaMM\n",
    "In this notebook we show how to modify existing models by swapping out submodels, and how to build your own model from scratch using existing submodels. To see all of the models and submodels available in PyBaMM, please take a look at the documentation [here](https://docs.pybamm.org/en/latest/source/api/models/index.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Changing a submodel in an existing battery model\n",
    "PyBaMM is designed to be a flexible modelling package that allows users to easily compare different models and numerical techniques within a common framework. Battery models within PyBaMM are built up using a number of submodels that describe different physics included within the model, such as mass conservation in the electrolyte or charge conservation in the solid. For ease of use, a number of popular battery models are pre-configured in PyBaMM. As an example, we look at the Single Particle Model (for more information see [here](SPM.ipynb)). \n",
    "\n",
    "First we import pybamm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "%pip install \"pybamm[plot,cite]\" -q    # install PyBaMM if it is not installed\n",
    "import pybamm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then we load the SPM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = pybamm.lithium_ion.SPM()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can look at the submodels that make up the SPM by accessing `model.submodels`, which is a dictionary of the submodel's name (i.e. the physics it represents) and the submodel that is selected"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "external circuit <pybamm.models.submodels.external_circuit.explicit_control_external_circuit.ExplicitCurrentControl object at 0x7f9cd479ea00>\n",
      "porosity <pybamm.models.submodels.porosity.constant_porosity.Constant object at 0x7f9cd479ecd0>\n",
      "Negative interface utilisation <pybamm.models.submodels.interface.interface_utilisation.full_utilisation.Full object at 0x7f9cd479ee80>\n",
      "Positive interface utilisation <pybamm.models.submodels.interface.interface_utilisation.full_utilisation.Full object at 0x7f9cd479eee0>\n",
      "negative particle mechanics <pybamm.models.submodels.particle_mechanics.no_mechanics.NoMechanics object at 0x7f9cd479ef40>\n",
      "positive particle mechanics <pybamm.models.submodels.particle_mechanics.no_mechanics.NoMechanics object at 0x7f9cd479efd0>\n",
      "negative primary active material <pybamm.models.submodels.active_material.constant_active_material.Constant object at 0x7f9cd47a60a0>\n",
      "positive primary active material <pybamm.models.submodels.active_material.constant_active_material.Constant object at 0x7f9cd47a6160>\n",
      "electrolyte transport efficiency <pybamm.models.submodels.transport_efficiency.bruggeman_transport_efficiency.Bruggeman object at 0x7f9cd479ed30>\n",
      "electrode transport efficiency <pybamm.models.submodels.transport_efficiency.bruggeman_transport_efficiency.Bruggeman object at 0x7f9cd47a61f0>\n",
      "transverse convection <pybamm.models.submodels.convection.transverse.no_convection.NoConvection object at 0x7f9cd47a6220>\n",
      "through-cell convection <pybamm.models.submodels.convection.through_cell.no_convection.NoConvection object at 0x7f9cd47a62b0>\n",
      "negative primary open-circuit potential <pybamm.models.submodels.interface.open_circuit_potential.single_ocp.SingleOpenCircuitPotential object at 0x7f9cd479ec40>\n",
      "positive primary open-circuit potential <pybamm.models.submodels.interface.open_circuit_potential.single_ocp.SingleOpenCircuitPotential object at 0x7f9cd479ed60>\n",
      "negative interface <pybamm.models.submodels.interface.kinetics.inverse_kinetics.inverse_butler_volmer.InverseButlerVolmer object at 0x7f9cd479ea90>\n",
      "negative interface current <pybamm.models.submodels.interface.kinetics.inverse_kinetics.inverse_butler_volmer.CurrentForInverseButlerVolmer object at 0x7f9cd479e880>\n",
      "positive interface <pybamm.models.submodels.interface.kinetics.inverse_kinetics.inverse_butler_volmer.InverseButlerVolmer object at 0x7f9cd479e610>\n",
      "positive interface current <pybamm.models.submodels.interface.kinetics.inverse_kinetics.inverse_butler_volmer.CurrentForInverseButlerVolmer object at 0x7f9cd479e6d0>\n",
      "negative primary particle <pybamm.models.submodels.particle.fickian_diffusion.FickianDiffusion object at 0x7f9cd479e3a0>\n",
      "negative primary total particle concentration <pybamm.models.submodels.particle.total_particle_concentration.TotalConcentration object at 0x7f9cd479e490>\n",
      "positive primary particle <pybamm.models.submodels.particle.fickian_diffusion.FickianDiffusion object at 0x7f9cd479e460>\n",
      "positive primary total particle concentration <pybamm.models.submodels.particle.total_particle_concentration.TotalConcentration object at 0x7f9cd479e2e0>\n",
      "negative electrode potential <pybamm.models.submodels.electrode.ohm.leading_ohm.LeadingOrder object at 0x7f9cd479e310>\n",
      "positive electrode potential <pybamm.models.submodels.electrode.ohm.leading_ohm.LeadingOrder object at 0x7f9cd49119a0>\n",
      "electrolyte diffusion <pybamm.models.submodels.electrolyte_diffusion.constant_concentration.ConstantConcentration object at 0x7f9cd4816d30>\n",
      "leading-order electrolyte conductivity <pybamm.models.submodels.electrolyte_conductivity.leading_order_conductivity.LeadingOrder object at 0x7f9cd4911fd0>\n",
      "negative surface potential difference <pybamm.models.submodels.electrolyte_conductivity.surface_potential_form.explicit_surface_form_conductivity.Explicit object at 0x7f9cd4911e50>\n",
      "positive surface potential difference <pybamm.models.submodels.electrolyte_conductivity.surface_potential_form.explicit_surface_form_conductivity.Explicit object at 0x7f9cd4911cd0>\n",
      "thermal <pybamm.models.submodels.thermal.isothermal.Isothermal object at 0x7f9cd4911bb0>\n",
      "current collector <pybamm.models.submodels.current_collector.homogeneous_current_collector.Uniform object at 0x7f9cd4911ac0>\n",
      "negative primary sei <pybamm.models.submodels.interface.sei.no_sei.NoSEI object at 0x7f9cd4907220>\n",
      "positive primary sei <pybamm.models.submodels.interface.sei.no_sei.NoSEI object at 0x7f9cd49078b0>\n",
      "negative primary sei on cracks <pybamm.models.submodels.interface.sei.no_sei.NoSEI object at 0x7f9cd4819880>\n",
      "positive primary sei on cracks <pybamm.models.submodels.interface.sei.no_sei.NoSEI object at 0x7f9cd4819610>\n",
      "negative lithium plating <pybamm.models.submodels.interface.lithium_plating.no_plating.NoPlating object at 0x7f9cd48e41c0>\n",
      "positive lithium plating <pybamm.models.submodels.interface.lithium_plating.no_plating.NoPlating object at 0x7f9cd48f66a0>\n",
      "total interface <pybamm.models.submodels.interface.total_interfacial_current.TotalInterfacialCurrent object at 0x7f9cd4911700>\n"
     ]
    }
   ],
   "source": [
    "for name, submodel in model.submodels.items():\n",
    "    print(name, submodel)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When you load a model in PyBaMM it builds by default. Building the model sets all of the model variables and sets up any variables which are coupled between different submodels: this is the process which couples the submodels together and allows one submodel to access variables from another. If you would like to swap out a submodel in an existing battery model you need to load it without building it by passing the keyword `build=False`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = pybamm.lithium_ion.SPM(build=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This collects all of the submodels which make up the SPM, but doesn't build the model. Now you are free to swap out one submodel for another. For instance, you may want to assume that diffusion within the negative particles is infinitely fast, so that the PDE describing diffusion is replaced with an ODE for the uniform particle concentration. To change a submodel you simply update the dictionary entry, in this case to the `XAveragedPolynomialProfile` submodel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.submodels[\"negative primary particle\"] = (\n",
    "    pybamm.particle.XAveragedPolynomialProfile(\n",
    "        model.param,\n",
    "        \"negative\",\n",
    "        options={**model.options, \"particle\": \"uniform profile\"},\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "where we pass in the model parameters, the electrode (negative or positive) the submodel corresponds to, and the name of the polynomial we want to use. In the example we assume uniform concentration within the particle, corresponding to a zero-order polynomial."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now if we look at the submodels again we see that the model for the negative particle has been changed"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "external circuit <pybamm.models.submodels.external_circuit.explicit_control_external_circuit.ExplicitCurrentControl object at 0x7f9cd48d94c0>\n",
      "porosity <pybamm.models.submodels.porosity.constant_porosity.Constant object at 0x7f9cd48cde80>\n",
      "Negative interface utilisation <pybamm.models.submodels.interface.interface_utilisation.full_utilisation.Full object at 0x7f9cd48cd970>\n",
      "Positive interface utilisation <pybamm.models.submodels.interface.interface_utilisation.full_utilisation.Full object at 0x7f9cd48cd850>\n",
      "negative particle mechanics <pybamm.models.submodels.particle_mechanics.no_mechanics.NoMechanics object at 0x7f9cd48cd790>\n",
      "positive particle mechanics <pybamm.models.submodels.particle_mechanics.no_mechanics.NoMechanics object at 0x7f9cd48cd520>\n",
      "negative primary active material <pybamm.models.submodels.active_material.constant_active_material.Constant object at 0x7f9cd48cd7c0>\n",
      "positive primary active material <pybamm.models.submodels.active_material.constant_active_material.Constant object at 0x7f9cd48cd460>\n",
      "electrolyte transport efficiency <pybamm.models.submodels.transport_efficiency.bruggeman_transport_efficiency.Bruggeman object at 0x7f9cd48cdb50>\n",
      "electrode transport efficiency <pybamm.models.submodels.transport_efficiency.bruggeman_transport_efficiency.Bruggeman object at 0x7f9cd491dc70>\n",
      "transverse convection <pybamm.models.submodels.convection.transverse.no_convection.NoConvection object at 0x7f9cd48b0ac0>\n",
      "through-cell convection <pybamm.models.submodels.convection.through_cell.no_convection.NoConvection object at 0x7f9cd48c48e0>\n",
      "negative primary open-circuit potential <pybamm.models.submodels.interface.open_circuit_potential.single_ocp.SingleOpenCircuitPotential object at 0x7f9cd481fd30>\n",
      "positive primary open-circuit potential <pybamm.models.submodels.interface.open_circuit_potential.single_ocp.SingleOpenCircuitPotential object at 0x7f9cd481f8e0>\n",
      "negative interface <pybamm.models.submodels.interface.kinetics.inverse_kinetics.inverse_butler_volmer.InverseButlerVolmer object at 0x7f9cd481fd00>\n",
      "negative interface current <pybamm.models.submodels.interface.kinetics.inverse_kinetics.inverse_butler_volmer.CurrentForInverseButlerVolmer object at 0x7f9cd481f9d0>\n",
      "positive interface <pybamm.models.submodels.interface.kinetics.inverse_kinetics.inverse_butler_volmer.InverseButlerVolmer object at 0x7f9cd481fa60>\n",
      "positive interface current <pybamm.models.submodels.interface.kinetics.inverse_kinetics.inverse_butler_volmer.CurrentForInverseButlerVolmer object at 0x7f9cd481f850>\n",
      "negative primary particle <pybamm.models.submodels.particle.x_averaged_polynomial_profile.XAveragedPolynomialProfile object at 0x7f9cd4952160>\n",
      "negative primary total particle concentration <pybamm.models.submodels.particle.total_particle_concentration.TotalConcentration object at 0x7f9cd481f6a0>\n",
      "positive primary particle <pybamm.models.submodels.particle.fickian_diffusion.FickianDiffusion object at 0x7f9cd481f7c0>\n",
      "positive primary total particle concentration <pybamm.models.submodels.particle.total_particle_concentration.TotalConcentration object at 0x7f9cd481f4c0>\n",
      "negative electrode potential <pybamm.models.submodels.electrode.ohm.leading_ohm.LeadingOrder object at 0x7f9cd481f550>\n",
      "positive electrode potential <pybamm.models.submodels.electrode.ohm.leading_ohm.LeadingOrder object at 0x7f9cd481f1c0>\n",
      "electrolyte diffusion <pybamm.models.submodels.electrolyte_diffusion.constant_concentration.ConstantConcentration object at 0x7f9cd48c4a60>\n",
      "leading-order electrolyte conductivity <pybamm.models.submodels.electrolyte_conductivity.leading_order_conductivity.LeadingOrder object at 0x7f9cd481fd90>\n",
      "negative surface potential difference <pybamm.models.submodels.electrolyte_conductivity.surface_potential_form.explicit_surface_form_conductivity.Explicit object at 0x7f9cd481fe20>\n",
      "positive surface potential difference <pybamm.models.submodels.electrolyte_conductivity.surface_potential_form.explicit_surface_form_conductivity.Explicit object at 0x7f9cd481fe80>\n",
      "thermal <pybamm.models.submodels.thermal.isothermal.Isothermal object at 0x7f9cd481fdf0>\n",
      "current collector <pybamm.models.submodels.current_collector.homogeneous_current_collector.Uniform object at 0x7f9cd481ff10>\n",
      "negative primary sei <pybamm.models.submodels.interface.sei.no_sei.NoSEI object at 0x7f9cd48330d0>\n",
      "positive primary sei <pybamm.models.submodels.interface.sei.no_sei.NoSEI object at 0x7f9cd4833100>\n",
      "negative primary sei on cracks <pybamm.models.submodels.interface.sei.no_sei.NoSEI object at 0x7f9cd48331f0>\n",
      "positive primary sei on cracks <pybamm.models.submodels.interface.sei.no_sei.NoSEI object at 0x7f9cd48331c0>\n",
      "negative lithium plating <pybamm.models.submodels.interface.lithium_plating.no_plating.NoPlating object at 0x7f9cd4833280>\n",
      "positive lithium plating <pybamm.models.submodels.interface.lithium_plating.no_plating.NoPlating object at 0x7f9cd48332e0>\n",
      "total interface <pybamm.models.submodels.interface.total_interfacial_current.TotalInterfacialCurrent object at 0x7f9cd481ff70>\n"
     ]
    }
   ],
   "source": [
    "for name, submodel in model.submodels.items():\n",
    "    print(name, submodel)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Building the model also sets up the equations, boundary and initial conditions for the model. For example, if we look at `model.rhs` before building we see that it is empty "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.rhs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If we try to use this empty model, PyBaMM will give an error. So, before proceeding we must build the model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.build_model()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now if we look at `model.rhs` we see that it contains an entry relating to the concentration in each particle, as expected for the SPM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{Variable(0x3825da4a5fc4eb0b, Discharge capacity [A.h], children=[], domains={}): Multiplication(0x7678edd47e530eec, *, children=['0.0002777777777777778', 'Current function [A]'], domains={}),\n",
       " Variable(-0x7fb8d0e6e9632372, Throughput capacity [A.h], children=[], domains={}): Multiplication(-0x7c65e8600b424661, *, children=['0.0002777777777777778', 'abs(Current function [A])'], domains={}),\n",
       " Variable(0x69f725db1a464db8, Average negative particle concentration [mol.m-3], children=[], domains={'primary': ['current collector']}): MatrixMultiplication(0xf98a766c86b2483, @, children=['mass(Average negative particle concentration [mol.m-3])', '-3.0 * Current function [A] / (Number of electrodes connected in parallel to make a cell * Electrode width [m] * Electrode height [m]) / Negative electrode thickness [m] / x-average(3.0 * Negative electrode active material volume fraction / Negative particle radius [m]) / Faraday constant [C.mol-1] / x-average(Negative particle radius [m])'], domains={'primary': ['current collector']}),\n",
       " Variable(0x48143b39c7603013, X-averaged positive particle concentration [mol.m-3], children=[], domains={'primary': ['positive particle'], 'secondary': ['current collector']}): Divergence(0x17c75a81711ad510, div, children=['Positive particle diffusivity [m2.s-1] * grad(X-averaged positive particle concentration [mol.m-3])'], domains={'primary': ['positive particle'], 'secondary': ['current collector']})}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.rhs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now the model can be used in a simulation and solved in the usual way, and we still have access to model defaults such as the default geometry and default spatial methods which are used in the simulation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "9a57346794d5451683e61d62b66d92fc",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(FloatSlider(value=0.0, description='t', max=1.0, step=0.01), Output()), _dom_classes=('w…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<pybamm.plotting.quick_plot.QuickPlot at 0x7f9cd496abe0>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simulation = pybamm.Simulation(model)\n",
    "simulation.solve([0, 3600])\n",
    "simulation.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Building a custom model from submodels\n",
    "Instead of editing a pre-existing model, you may wish to build your own model from scratch by combining existing submodels of you choice. In this section, we build a Single Particle Model in which the diffusion is assumed infinitely fast in both particles. \n",
    "\n",
    "To begin, we load a base lithium-ion model. This sets up the basic model structure behind the scenes, and also sets the default parameters to be those corresponding to a lithium-ion battery. Note that the base model does not select any default submodels, so there is no need to pass `build=False`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = pybamm.lithium_ion.BaseModel()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Submodels can be added to the `model.submodels` dictionary in the same way that we changed the submodels earlier. \n",
    "\n",
    "We use the simplest model for the external circuit, which is the explicit \"current control\" submodel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.submodels[\"external circuit\"] = pybamm.external_circuit.ExplicitCurrentControl(\n",
    "    model.param, model.options\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We want to build a 1D model, so select the `Uniform` current collector model (if the current collectors are behaving uniformly, then a 1D model is appropriate). We also want the model to be isothermal, so select the thermal model accordingly. Further, we assume that the porosity and active material are constant in space and time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.submodels[\"current collector\"] = pybamm.current_collector.Uniform(model.param)\n",
    "model.submodels[\"thermal\"] = pybamm.thermal.isothermal.Isothermal(model.param)\n",
    "model.submodels[\"porosity\"] = pybamm.porosity.Constant(model.param, model.options)\n",
    "model.submodels[\"negative active material\"] = pybamm.active_material.Constant(\n",
    "    model.param, \"negative\", model.options\n",
    ")\n",
    "model.submodels[\"positive active material\"] = pybamm.active_material.Constant(\n",
    "    model.param, \"positive\", model.options\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We assume that the current density varies linearly in the electrodes. This corresponds the the leading-order terms in Ohm's law in the limit in which the SPM is derived in [[3]](#References)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.submodels[\"negative electrode potentials\"] = pybamm.electrode.ohm.LeadingOrder(\n",
    "    model.param, \"negative\"\n",
    ")\n",
    "model.submodels[\"positive electrode potentials\"] = pybamm.electrode.ohm.LeadingOrder(\n",
    "    model.param, \"positive\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We assume uniform concentration in both the negative and positive particles. We also have to separately specify a model for the total concentration in each electrode, which is calculated from the concentration in the particles (not a separate ODE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "options = {**model.options, \"particle\": \"uniform profile\"}\n",
    "model.submodels[\"negative primary particle\"] = (\n",
    "    pybamm.particle.XAveragedPolynomialProfile(model.param, \"negative\", options)\n",
    ")\n",
    "model.submodels[\"positive primary particle\"] = (\n",
    "    pybamm.particle.XAveragedPolynomialProfile(model.param, \"positive\", options)\n",
    ")\n",
    "\n",
    "model.submodels[\"negative total particle concentration\"] = (\n",
    "    pybamm.particle.TotalConcentration(model.param, \"negative\", options)\n",
    ")\n",
    "model.submodels[\"positive total particle concentration\"] = (\n",
    "    pybamm.particle.TotalConcentration(model.param, \"positive\", options)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the Single Particle Model, the overpotential can be obtained by inverting the Butler-Volmer relation, so we choose the `InverseButlerVolmer` submodel for the interface, with the \"main\" lithium-ion reaction (and default lithium ion options). Because of how the current is implemented, we also need to separately specify the `CurrentForInverseButlerVolmer` submodel. We also need to specify the submodel for open-circuit potential."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.submodels[\"negative open-circuit potential\"] = (\n",
    "    pybamm.open_circuit_potential.SingleOpenCircuitPotential(\n",
    "        model.param, \"negative\", \"lithium-ion main\", options=model.options\n",
    "    )\n",
    ")\n",
    "model.submodels[\"positive open-circuit potential\"] = (\n",
    "    pybamm.open_circuit_potential.SingleOpenCircuitPotential(\n",
    "        model.param, \"positive\", \"lithium-ion main\", options=model.options\n",
    "    )\n",
    ")\n",
    "model.submodels[\"negative interface\"] = pybamm.kinetics.InverseButlerVolmer(\n",
    "    model.param, \"negative\", \"lithium-ion main\", options=model.options\n",
    ")\n",
    "model.submodels[\"positive interface\"] = pybamm.kinetics.InverseButlerVolmer(\n",
    "    model.param, \"positive\", \"lithium-ion main\", options=model.options\n",
    ")\n",
    "model.submodels[\"negative interface current\"] = (\n",
    "    pybamm.kinetics.CurrentForInverseButlerVolmer(\n",
    "        model.param, \"negative\", \"lithium-ion main\"\n",
    "    )\n",
    ")\n",
    "model.submodels[\"positive interface current\"] = (\n",
    "    pybamm.kinetics.CurrentForInverseButlerVolmer(\n",
    "        model.param, \"positive\", \"lithium-ion main\"\n",
    "    )\n",
    ")\n",
    "model.submodels[\"negative interface utilisation\"] = pybamm.interface_utilisation.Full(\n",
    "    model.param, \"negative\", model.options\n",
    ")\n",
    "model.submodels[\"positive interface utilisation\"] = pybamm.interface_utilisation.Full(\n",
    "    model.param, \"positive\", model.options\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We don't want any particle mechanics, SEI formation or lithium plating in this model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.submodels[\"Negative particle mechanics\"] = pybamm.particle_mechanics.NoMechanics(\n",
    "    model.param, \"negative\", model.options\n",
    ")\n",
    "model.submodels[\"Positive particle mechanics\"] = pybamm.particle_mechanics.NoMechanics(\n",
    "    model.param, \"positive\", model.options\n",
    ")\n",
    "model.submodels[\"Negative sei\"] = pybamm.sei.NoSEI(\n",
    "    model.param, \"negative\", model.options\n",
    ")\n",
    "model.submodels[\"Positive sei\"] = pybamm.sei.NoSEI(\n",
    "    model.param, \"positive\", model.options\n",
    ")\n",
    "model.submodels[\"Negative sei on cracks\"] = pybamm.sei.NoSEI(\n",
    "    model.param, \"negative\", model.options, cracks=True\n",
    ")\n",
    "model.submodels[\"Positive sei on cracks\"] = pybamm.sei.NoSEI(\n",
    "    model.param, \"positive\", model.options, cracks=True\n",
    ")\n",
    "model.submodels[\"Negative lithium plating\"] = pybamm.lithium_plating.NoPlating(\n",
    "    model.param, \"negative\"\n",
    ")\n",
    "model.submodels[\"Positive lithium plating\"] = pybamm.lithium_plating.NoPlating(\n",
    "    model.param, \"positive\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, for the electrolyte we assume that diffusion is infinitely fast so that the concentration is uniform, and also use the leading-order model for charge conservation, which leads to a linear variation in ionic current in the electrodes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.submodels[\"electrolyte diffusion\"] = (\n",
    "    pybamm.electrolyte_diffusion.ConstantConcentration(model.param)\n",
    ")\n",
    "model.submodels[\"electrolyte conductivity\"] = (\n",
    "    pybamm.electrolyte_conductivity.LeadingOrder(model.param)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now that we have set all of the submodels we can build the model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.build_model()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can then use the model in a simulation in the usual way"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "36ae60e068de4c6a9a61697535abb080",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "interactive(children=(FloatSlider(value=0.0, description='t', max=1.0, step=0.01), Output()), _dom_classes=('w…"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "<pybamm.plotting.quick_plot.QuickPlot at 0x7f9ccfd7ba30>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "simulation = pybamm.Simulation(model)\n",
    "simulation.solve([0, 3600])\n",
    "simulation.plot()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## References\n",
    "\n",
    "The relevant papers for this notebook are:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1] Joel A. E. Andersson, Joris Gillis, Greg Horn, James B. Rawlings, and Moritz Diehl. CasADi – A software framework for nonlinear optimization and optimal control. Mathematical Programming Computation, 11(1):1–36, 2019. doi:10.1007/s12532-018-0139-4.\n",
      "[2] Charles R. Harris, K. Jarrod Millman, Stéfan J. van der Walt, Ralf Gommers, Pauli Virtanen, David Cournapeau, Eric Wieser, Julian Taylor, Sebastian Berg, Nathaniel J. Smith, and others. Array programming with NumPy. Nature, 585(7825):357–362, 2020. doi:10.1038/s41586-020-2649-2.\n",
      "[3] Scott G. Marquis, Valentin Sulzer, Robert Timms, Colin P. Please, and S. Jon Chapman. An asymptotic derivation of a single particle model with electrolyte. Journal of The Electrochemical Society, 166(15):A3693–A3706, 2019. doi:10.1149/2.0341915jes.\n",
      "[4] Venkat R. Subramanian, Vinten D. Diwakar, and Deepak Tapriyal. Efficient macro-micro scale coupled modeling of batteries. Journal of The Electrochemical Society, 152(10):A2002, 2005. doi:10.1149/1.2032427.\n",
      "[5] Valentin Sulzer, Scott G. Marquis, Robert Timms, Martin Robinson, and S. Jon Chapman. Python Battery Mathematical Modelling (PyBaMM). Journal of Open Research Software, 9(1):14, 2021. doi:10.5334/jors.309.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "pybamm.print_citations()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  },
  "vscode": {
   "interpreter": {
    "hash": "187972e187ab8dfbecfab9e8e194ae6d08262b2d51a54fa40644e3ddb6b5f74c"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
